import { createRouter, createWebHistory } from 'vue-router'
import cookies from 'vue-cookies'
import store from 'vuex'
import { defineAsyncComponent, onMounted } from 'vue'

import jm_util from '../assets/utils/base'
import { setTimeout } from 'core-js'

import NotFound from '@/views/Error.vue'

const routes = [
  {
    path: '/',
    name: 'login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/admin',
    name: 'admin',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/admin/frame.vue'),
    children: [
      // 模板页路径
      {
        path: '/admin/modal',
        name: 'admin_modal',
        meta: {
          isMenu: true,
          title: '模板页',
          componentUrl: defineAsyncComponent(() => {
            return import('@/views/admin/z_Modal/index.vue')
          })
        },
        component: () => import('@/views/admin/z_Modal/index.vue')
      },
      // 订单管理
      {
        path: '/admin/order',
        name: 'order',
        meta: {
          isMenu: true,
          title: '订单管理',
          componentUrl: defineAsyncComponent(() => {
            return import('@/views/admin/a_order/frame.vue')
          })
        },
        component: () => import('@/views/admin/a_order/frame.vue')
      },

      
      // 配比方案
      {
        path: '/admin/programme',
        name: 'programme',
        meta: {
          isMenu: true,
          title: '配比方案',
          componentUrl: defineAsyncComponent(() => {
            return import('@/views/admin/b_programme/index.vue')
          })
        },
        component: () => import('@/views/admin/b_programme/index.vue')
      },
      // 采购方案
      {
        path: '/admin/buyplan',
        name: 'buyplan',
        meta: {
          isMenu: true,
          title: '采购方案',
          componentUrl: defineAsyncComponent(() => {
            return import('@/views/admin/c_buyplan/index.vue')
          })
        },
        component: () => import('@/views/admin/c_buyplan/index.vue')
      },
      // 采购任务单
      {
        path: '/admin/buymission',
        name: 'buymission',
        meta: {
          isMenu: true,
          title: '采购任务单',
          componentUrl: defineAsyncComponent(() => {
            return import('@/views/admin/d_buymission/index.vue')
          })
        },
        component: () => import('@/views/admin/d_buymission/index.vue')
      },
      // 裁剪方案
      {
        path: '/admin/cutplan',
        name: 'cutplan',
        meta: {
          isMenu: true,
          title: '裁剪方案',
          componentUrl: defineAsyncComponent(() => {
            return import('@/views/admin/e_cutplan/index.vue')
          })
        },
        component: () => import('@/views/admin/e_cutplan/index.vue')
      },
      // 裁剪任务单
      {
        path: '/admin/cutmission',
        name: 'cutmission',
        meta: {
          isMenu: true,
          title: '裁剪任务单',
          componentUrl: defineAsyncComponent(() => {
            return import('@/views/admin/f_cutmission/index.vue')
          })
        },
        component: () => import('@/views/admin/f_cutmission/index.vue')
      },


      // 模板页路径
      {
        path: '/admin/modal2_0',
        name: 'modal2_0',
        meta: {
          isMenu: true,
          title: '测试2',
          componentUrl: defineAsyncComponent(() => {
            return import('@/views/admin/z_Modal2_0/index.vue')
          })
        },
        component: () => import('@/views/admin/z_Modal2_0/index.vue')
      }

    ]
  },






  // 404 路由，放在最后
  { path: '/:pathMatch(.*)', component: NotFound }
  
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})



// let registerRouteFresh = true; //防止多次调用添加路由
router.beforeEach((to, from, next) => {
  let strpath = to.fullPath
  // console.log(to)
  if (to.meta.requireAuth) {
    // 是否登录 cookies.get('token')
    // if (true) {
      
    if (cookies.get('token') || true) {
      // if (registerRouteFresh) {
      //   generateRoutes(menu).forEach(item => {
      //     router.addRoute(item)
      //   })
      //   registerRouteFresh = false
      // }
      if (to.meta.isMenu) {
        store.useStore().commit('set_menuTabs', {
          name: to.name,
          path: to.path,
          title: to.meta.title?to.meta.title:'',
          componentUrl: to.meta.componentUrl?to.meta.componentUrl:'',
        })
        // console.log(store.useStore().state.menuTag)
      }
      next()
    } else {
      next({
        path: '/',
        query: { redirect: strpath }
      })
    }
  } else {
    // console.log('没有验证')
    next()
  }
})

export default router
